<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ReviewerGroupProject extends Model
{
    protected $guarded = [];
    public $timestamps = false;

    // 专家组评分状态
    const SCORE_STATUS_TO_SCORE = 1;
    const SCORE_STATUS_SCORE_FINISHED = 2;

    // 2020-06-03, 已废弃
    const SCORE_STATUS_ABNORMAL_SCORES = 2;
    const SCORE_STATUS_TO_LEADER_REVIEW = 3;
    const SCORE_STATUS_LEADER_REVIEW_PASSED = 4;
    const SCORE_STATUS_LEADER_REVIEW_REJECTED = 5;

    const SCORE_STATUS_PHRASES = [
        self::SCORE_STATUS_TO_SCORE => '待评分',
        self::SCORE_STATUS_SCORE_FINISHED => '评分已完成',

        // 2020-06-03, 已废弃
        //self::SCORE_STATUS_ABNORMAL_SCORES => '分值异常',
        self::SCORE_STATUS_TO_LEADER_REVIEW => '待组长审核',
        self::SCORE_STATUS_LEADER_REVIEW_PASSED => '组长审核通过',
        self::SCORE_STATUS_LEADER_REVIEW_REJECTED => '组长审核不通过',
    ];

    // 专家组各专家的评分分值状态
    const SCORES_DESC_TO_SCORE = '待评分';
    const SCORES_DESC_NORMAL = '正常';
    const SCORES_DESC_ZERO_SCORE = '零分';
    const SCORES_DESC_BIG_DIFF = '分差较大';

    // 大组长对专家组评审结果的审核结果
    const LEADER_REVIEW_RESULT_PASSED = '通过';
    const LEADER_REVIEW_RESULT_REJECTED = '不通过';

    public static function getScoreStatusPhraseById($scoreStatusId) {
        /**
         * @edit
         * @time 2020-06-10
         * @需求 专家组评分状态仅为 待评分｜评分已完成，移除其他状态
         */
        return $scoreStatusId == self::SCORE_STATUS_TO_SCORE
            ? self::SCORE_STATUS_PHRASES[$scoreStatusId]
            : self::SCORE_STATUS_PHRASES[self::SCORE_STATUS_SCORE_FINISHED];
        //return self::SCORE_STATUS_PHRASES[$scoreStatusId];
    }

    /**
     * @param $scores array[int|null]
     * @return array
     */
    public static function calcScoresState(array $scores)
    {
        foreach ($scores as $score) {
            if ($score === null) {
                return [
                    'ok' => true,
                    'desc' => self::SCORES_DESC_TO_SCORE,
                ];
            }
        }
        sort($scores);
        $max = end($scores);
        $min = $scores[0];
        if ($min == 0) {
            return [
                'ok' => false,
                'desc' => self::SCORES_DESC_ZERO_SCORE
            ];
        } elseif ($max - $min >= $min * 0.3) {
            return [
                'ok' => false,
                'desc' => self::SCORES_DESC_BIG_DIFF
            ];
        } else {
            return [
                'ok' => true,
                'desc' => self::SCORES_DESC_NORMAL
            ];
        }
    }
}
